/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aic.ws;

import aic.beans.Address;
import aic.beans.Customer;
import aic.beans.Item;
import aic.beans.Order;
import aic.beans.Payment_Preference;
import aic.faults.ItemUnavailableFault;
import aic.faults.ItemUnavailableFaultFactory;
import aic.faults.UnknownCustomerFault;
import aic.faults.UnknownCustomerFaultFactory;
import aic.faults.UnknownItemFault;
import aic.faults.UnknownItemFaultFactory;
import aic.faults.UnknownOrderFault;
import aic.faults.UnknownOrderFaultFactory;
import aic.lists.AddressList;
import aic.lists.AvailableItemsList;
import aic.lists.CustomerList;
import aic.lists.OrderList;
import aic.lists.ShippingList;
import aic.misc.DebugPrint;
import javax.jws.WebService;

@WebService(portName = "OrderManagementPT", endpointInterface = "aic.ws.OrderManagementService", serviceName = "OrderManagementService")
public class OrderManagementServiceImpl implements OrderManagementService {

    public boolean check_availability(Item item) throws UnknownItemFault {

        DebugPrint.println("entering check_availability");

        try {
            if (AvailableItemsList.isAvailable(item)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            UnknownItemFault uif = UnknownItemFaultFactory.createSOAPUnknownItemFault("http://bittschen.net/OrderManagementService/check_availability", item);
            throw uif;
//            return false;
        }
    }

    public Order place_order(Item[] items, Customer customer) throws UnknownCustomerFault, ItemUnavailableFault {

        DebugPrint.println("entering place_order");

        if (!CustomerList.contains(customer)) {
            UnknownCustomerFault ucf = UnknownCustomerFaultFactory.createSOAPUnknownCustomerFault("http://bittschen.net/OrderManagementService/place_order", customer);
            throw ucf;
        }

        try {
            Order order = AvailableItemsList.placeOrder(items, customer);

            return order;
        } catch (Exception ex) {
            ItemUnavailableFault iuf = ItemUnavailableFaultFactory.createSOAPItemUnavailableFault("http://bittschen.net/OrderManagementService/place_order", null);
            throw iuf;
        }
    }

    public boolean is_finished(Order order) throws UnknownOrderFault {

        DebugPrint.println("checking is_finished");

        if (!OrderList.contains(order)) {
            UnknownOrderFault uof = UnknownOrderFaultFactory.createSOAPUnknownOrderFault("http://bittschen.net/OrderManagementService/place_order", order);
            throw uof;
        }

        return ShippingList.is_finished(order);
    }

    public boolean insert_test_data() {

        DebugPrint.println("inserting test data..");
        
        Address address1 = new Address();
        address1.setCity("Vienna");
        address1.setDoor(24);
        address1.setHouse(12);
        address1.setStreet("MyStreet");
        address1.setZip_code("1234");

        Address address2 = new Address();
        address2.setCity("Vienna");
        address2.setDoor(43);
        address2.setHouse(5);
        address2.setStreet("YourStreet");
        address2.setZip_code("1243");

        AddressList.add(address1);
        AddressList.add(address2);

        Customer customer1 = new Customer("6980517a-c777-4c61-99c1-bfa0ad848889", "Hans Müller", address1, Payment_Preference.Bank_Transfer);
        Customer customer2 = new Customer("d5596197-f737-49dd-8e68-d9be313f832c", "Franziska O.", address2, Payment_Preference.Credit_Card);

        CustomerList.add(customer1);
        CustomerList.add(customer2);

        // create items
        Item item1 = new Item(3565, "7980517a-c777-4c61-99c1-bfa0ad848889", 9.90);
        Item item2 = new Item(1218, "dafc1d06-3e7e-4d12-b645-d793e1b9f24c", 14.90);
        Item item3 = new Item(2797, "ef540734-0a47-454e-b850-63bf1acf44e9", 2.99);

        AvailableItemsList.add(item1);
        AvailableItemsList.add(item2);
        AvailableItemsList.add(item3);

        DebugPrint.println("..done inserting test data. Here comes the list of inserted items: ");

        AddressList.printList();
        CustomerList.printList();
        AvailableItemsList.printList();

        return true;
    }
}
